/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.attitudes;

import ore.Attitude;
import ore.Frame;
import ore.PositionVelocity;
import ore.errors.OrekitException;
import ore.time.AbsoluteDate;

import org.apache.commons.math.geometry.Rotation;
import org.apache.commons.math.geometry.Vector3D;

/**
 * Compensation modes on ground pointing attitudes.
 * 
 * @author V&eacute;ronique Pommier-Maurussane
 * @author Luc Maisonobe
 */
public abstract class GroundPointingCompensating
    extends GroundPointing
{

    public final GroundPointing groundPointingLaw;

    /** 
     * @param groundPointingLaw Ground pointing attitude law without compensation
     */
    public GroundPointingCompensating(GroundPointing groundPointingLaw) {
        super(groundPointingLaw.getBodyFrame());
        this.groundPointingLaw = groundPointingLaw;
    }


    /** 
     * @return Attitude satellite rotation from reference frame.
     */
    public GroundPointing getGroundPointingLaw() {
        return this.groundPointingLaw;
    }
    /** 
     * @param date computation date.
     * @param pv satellite position-velocity vector at given date in given frame.
     * @param frame the frame in which satellite position-velocity is given.
     * 
     * @return Target position/velocity in body frame at date.
     * 
     * @throws OrekitException if some specific error occurs
     */
    protected PositionVelocity getTargetInBodyFrame(AbsoluteDate date, PositionVelocity pv, Frame frame)
        throws OrekitException
    {
        /*
         * Return basic attitude law target
         */
        return this.groundPointingLaw.getTargetInBodyFrame(date, pv, frame);
    }
    /** 
     * @param date date when the system state shall be computed
     * @param pv satellite position-velocity vector at given date in given frame.
     * @param frame the frame in which satellite position-velocity is given.
     * 
     * @return Satellite base attitude state, i.e. without yaw compensation.
     * 
     * @throws OrekitException if some specific error occurs
     */
    public Attitude getBaseState(AbsoluteDate date, PositionVelocity pv, Frame frame)
        throws OrekitException
    {
        return this.groundPointingLaw.getState(date, pv, frame);
    }
    /** 
     * @param date date when the system state shall be computed
     * @param pv satellite position-velocity vector at given date in given frame.
     * @param frame the frame in which satellite position-velocity is given.
     * 
     * @return Satellite attitude state at date, in given frame, with
     * yaw compensation.
     * 
     * @throws OrekitException if some specific error occurs
     */
    public Attitude getState(AbsoluteDate date, PositionVelocity pv, Frame frame)
        throws OrekitException
    {
        /*
         * Get attitude from base attitude law
         */
        Attitude base = getBaseState(date, pv, frame);
        /*
         * Get yaw compensation
         */
        Rotation compensation = this.getCompensation(date, pv, base, frame);

        Rotation attitude = compensation.applyTo(base.getRotation());

        Vector3D spin = compensation.applyTo(base.getSpin());
        /*
         * Combination of base attitude and yaw compensation
         */
        return new Attitude(frame, attitude, spin);
    }
    /** 
     * @param date Date when the system state shall be computed
     * @param pv Satellite position-velocity vector at given date in
     * given frame.
     * @param base Base satellite attitude in given frame.
     * @param frame The frame in which satellite position-velocity an
     * attitude are given.
     * 
     * @return Compensation rotation at date, i.e. rotation between non
     * compensated attitude state and compensated state.
     * 
     * @throws OrekitException if some specific error occurs
     */
    public abstract Rotation getCompensation(AbsoluteDate date, PositionVelocity pv,
                                             Attitude base, Frame frame)
        throws OrekitException;

}
